package com.simulation.demo.service;

import com.simulation.demo.config.ApplicationConfigure;
import com.simulation.demo.entity.ExportEntity;
import com.simulation.demo.excel.ExportExcel;
import com.simulation.demo.excel.ImportExcel;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author buddy
 * @date 2021/7/30
 */
@Service
public class ExcelService {
    @Autowired
    private ApplicationConfigure configure;

    public String exportExcel() {
        List<ExportEntity> dataList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            ExportEntity exportEntity = new ExportEntity(i, i + "");
            dataList.add(exportEntity);
        }
        ExportExcel exportExcel = new ExportExcel("demo_title", "demo_sheet", ExportEntity.class);
        exportExcel.setDataList(dataList);

        String filePath = configure.getLocalFileBasePath() + "abc.xlsx";
        exportExcel.writeFile(filePath);
        return filePath.replace(configure.getLocalFileBasePath(), configure.getWebUrlBasePath());
    }

    public List<ExportEntity> importExcel() {
        String excelFilePath = "/Users/buddy/Desktop/work/youson/workspace/simulation-demo/src/main/resources/abc.xlsx";
        List<ExportEntity> list = null;
        try {
            ImportExcel importExcel = new ImportExcel(excelFilePath, 1, 0);
            list = importExcel.getDataList(ExportEntity.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
